package com.xr.elm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.elm.common.CustomException;
import com.xr.elm.dto.SetmealDto;
import com.xr.elm.entity.Dish;
import com.xr.elm.entity.Setmeal;
import com.xr.elm.entity.SetmealDish;
import com.xr.elm.mapper.SetmealMapper;
import com.xr.elm.service.SetmealDishService;
import com.xr.elm.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行inserti操作I
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes= setmealDishes.stream().peek((item)-> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish，执行inserti操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //根据菜品基本信息，从dish表查询
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前菜品对应的口味信息，从dish_flavor查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新dish表信息
        this.updateById(setmealDto);
        //清理当前套餐
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes= setmealDishes.stream().peek((item)-> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish，执行inserti操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
//select count (*from setmeal where id in (1,2,3)and status =1
//        查询套餐状态，确定是否可用刚除如果不能删除，
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);

//        抛出一个业务异常如果可以删除，
        if (count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
//        先删除套餐表中的数据——setmeal
        this.removeByIds(ids);
//          删除关系表中的数据--setmeal_dish
        //delete from setmealdish where setmeal id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);
    }

    @Override
    public void sellWithDish(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Setmeal::getId,ids);
        lambdaUpdateWrapper.set(Setmeal::getStatus,status);
        setmealMapper.update(null,lambdaUpdateWrapper);
    }

}

